package com.train.batch.job;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.train.daily.api.DailyTrainApi;
import com.train.common.enums.impl.BizExceptionEnums;
import com.train.common.resp.CommonResp;
import com.train.common.status.HttpStatus;
import com.train.common.utils.date.LocalDateTimeUtils;
import com.train.quartz.handler.MyJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

@Slf4j
@Component
public class DailyTrainJob implements MyJob {

    @Autowired
    private DailyTrainApi dailyTrainApi;

    @Override
    public void execute(Object param) {
        //用当前时间生成15天后的时间
        String offsetDate = LocalDateTimeUtil
                .offset(LocalDateTimeUtils.getNow(), 15, ChronoUnit.DAYS)
                .format(DateTimeFormatter.ofPattern(LocalDateTimeUtils.DATE_FORMAT));
        //生成每日车次
        CommonResp resp = dailyTrainApi.generateDailyTrain(offsetDate);
        if (resp.get(CommonResp.CODE_TAG).equals(HttpStatus.ERROR)) {
            Object msg = resp.get(CommonResp.MESSAGE_TAG);
            log.error("{}", msg != null ? msg : BizExceptionEnums.EXP_DAILY_TRAIN_GENERATE.getMessage());

            //保存到异常日志

        }
    }
}
